package com.xueqiu.lovepetmedicalmanagersystem.controller;

import com.xueqiu.lovepetmedicalmanagersystem.pojo.Owners;
import com.xueqiu.lovepetmedicalmanagersystem.service.OwnersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * @author xueqiu
 */
@Controller
@RequestMapping("/own")
public class OwnersController {

    @Autowired
    private OwnersService ownersService;

    /**
     * 宠物主人更新，先执行查询宠物主人功能
     * @param ownName 宠物主人姓名
     * @param model 模型参数
     * @return 查询宠物主人成功，跳转到更新页面；查询宠物主人失败，跳转到宠物搜索结果页面；
     */
    @RequestMapping("/toOwnUpdate")
    public String toOwnUpdate(String ownName, Model model) {
        Owners owners = ownersService.queryOwnerByName(ownName);
        if (owners != null) {
            model.addAttribute("owners", owners);
            model.addAttribute("msg", "查询成功！！！");
            return "pet/ownerupdate";
        } else {
            model.addAttribute("msg", "查询错误！！！");
            return "pet/petresult";
        }
    }

    /**
     * 宠物主人更新
     * @param owners 数据源
     * @param model 模型参数
     * @return 更新成功则跳转到宠物搜索页面；更新失败则跳转到宠物主人更新页面
     */
    @RequestMapping("/ownUpdate")
    public String ownUpdate(Owners owners, Model model) {

        Integer i = ownersService.ownUpdate(owners);

        if (i > 0) {
            model.addAttribute("owners", owners);
            model.addAttribute("msg", "主人信息修改成功！！！");
            return "pet/petsearch";
        } else {
            model.addAttribute("msg", "主人信息修改失败！！！");
            return "pet/ownerupdate";
        }
    }

    /**
     * 跳转控制器；从宠物更新页面退出到宠物查询页面
     * @param model 模型参数
     * @return 放弃更新宠物主人，跳转到宠物搜索页面
     */
    @RequestMapping("/abandonUpdate")
    public String abandonUpdate(Model model) {
        model.addAttribute("msg", "放弃成功！！！");
        return "pet/petsearch";
    }

    /**
     * 宠物主人新增
     * @param owners 数据源
     * @param model 模型参数
     * @return 新增成功则跳转到宠物搜索页面；新增失败则跳转到宠物主人新增页面
     */
    @RequestMapping("/insertOwn")
    public String insertOwn(Owners owners, Model model) {
        Integer i = ownersService.insertOwn(owners);
        if (i > 0) {
            model.addAttribute("msg", "主人信息添加成功！！！");
            return "pet/petsearch";
        } else {
            model.addAttribute("msg", "主人信息添加失败！！！");
            return "pet/ownersadd";
        }
    }
}
